package com.cx.bank.view;

import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 17:08
 */
public class BankRegisterView extends JFrame {
    private JButton register;
    private JLabel idLabel, codeLabel, moneyLabel, bank;
    public JTextField idText, montyText;
    private JPasswordField codeTest;

    public BankRegisterView(){
        building();
        addlisteners();
    }



    private void building() {
        setLayout(null);
        //标签
        bank = new JLabel("bank");
        idLabel = new JLabel("账号");
        codeLabel = new JLabel("密码");
        moneyLabel = new JLabel("开户");
        //按钮
        register = new JButton("注册");

        idText = new JTextField(20);
        montyText = new JTextField(20);
        codeTest = new JPasswordField(20);
        bank.setBounds(300,20,120,50);
        Font font = new Font("圆体", Font.BOLD, 50);
        Font font1 = new Font("圆体", Font.BOLD, 30);
        bank.setFont(font);
        register.setBounds(150, 500, 120, 25);
        idLabel.setBounds(150, 250, 120, 25);
        idLabel.setFont(font1);
        codeLabel.setBounds(150, 325, 120, 25);
        codeLabel.setFont(font1);
        moneyLabel.setBounds(150, 400, 120, 25);
        moneyLabel.setFont(font1);
        idText.setBounds(250, 250, 200, 30);
        codeTest.setBounds(250, 325, 200, 30);
        montyText.setBounds(250, 400, 200, 30);


        add(bank);
        add(idLabel);
        add(idText);
        add(codeLabel);
        add(codeTest);
        add(moneyLabel);
        add(montyText);
        add(register);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserDaoFactory userDaoFactory = null;
                try {
                    userDaoFactory = UserDaoFactory.getInstance();
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException ex) {
                    ex.printStackTrace();
                }
                ManagerImpl manager = userDaoFactory.getManager();
                UserBean userBean = null;

                String userName = idText.getText();
                Integer password = Integer.valueOf(codeTest.getText());
                MoneyBean moneyBean = new MoneyBean(Double.valueOf(montyText.getText()));
                userBean = new UserBean(password, moneyBean, userName);
//                manager.setUserBean(userBean);
                try {
                    boolean registerStatus = manager.register(userBean);
                    if (registerStatus){
                        //注册成功，关闭当前页面
                        BankRegisterView.this.dispose();
                        //打开BankView
                        new BankView().setVisible(true);
                    }else {
                        JOptionPane.showMessageDialog(register, "注册失败", "标题", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }
}
